package com.pussinboots.morning.cms.modules.goods.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pussinboots.morning.cms.common.security.AuthorizingUser;
import com.pussinboots.morning.cms.common.util.ResultBean;
import com.pussinboots.morning.cms.common.util.SingletonLoginUtils;
import com.pussinboots.morning.cms.modules.system.product.entity.OsProduct;
import com.pussinboots.morning.cms.modules.system.product.entity.OsProductDetail;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductAttributeService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductCategoryService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductDetailService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductImageService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductParameterService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductSpecificationService;
import com.pussinboots.morning.cms.modules.system.product.vo.OsProductVo;
import com.pussinboots.morning.common.controller.BaseController;

/**
 * 
* 项目名称：morning-cms-webapp   
* 类名称：SystemVersionLogControlller   
* 类描述：版本日至表示层控制器    
* 创建人：陈星星   
* 创建时间：2017年2月17日 上午12:32:07
 */
@Controller
@RequestMapping(value = "/system/goods")
public class GoodsListProductControlller extends BaseController {
	
	/** 管理员列表 */
	private static final String SYSTEM_GOODS_LIST_SET_PRODUCT= getViewPath("modules/goods/system_goods_list_set_product");
	
	
	@Autowired
	private IOsProductService productService;
	@Autowired
	private IOsProductImageService productImageService;
	@Autowired
	private IOsProductDetailService productDetailService;
	@Autowired
	private IOsProductCategoryService productCategoryService;
	@Autowired
	private IOsProductParameterService productParameterService;
	@Autowired
	private IOsProductAttributeService productAttributeService;
	@Autowired
	private IOsProductSpecificationService productSpecificationService;
	
	/**
	 * 跳转道添加商品页面
	 * @return
	 */
	//@RequiresPermissions("system:goods:list:add")
	@GetMapping(value = "/list/add")
	public String create(Model model) {
		return SYSTEM_GOODS_LIST_SET_PRODUCT;
	}
	
	/**
	 * 打开商品编辑页面
	 * @param model
	 * @param productId
	 * @return
	 */
	//@RequiresPermissions("system:goods:list:edit")
	@GetMapping(value = {"/list/edit/{id}" })
	public String edit(Model model,@PathVariable("id") Long productId ) {
		
		OsProductVo osProductVo = new OsProductVo();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("product_id", productId);
		
		OsProduct oPentity = productService.selectById(productId);
		
		EntityWrapper<OsProductDetail> wrapper = new EntityWrapper<OsProductDetail>();
		wrapper.where("product_id={0}",productId); 
		OsProductDetail detail = productDetailService.selectOne(wrapper);
		if(detail!=null){
			osProductVo.setDescription(detail.getDescription());
		}
		
		try {
			PropertyUtils.copyProperties(osProductVo, oPentity);
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			e.printStackTrace();
		}
		model.addAttribute("osProductVo",osProductVo);
		
		return SYSTEM_GOODS_LIST_SET_PRODUCT;
	}
	
	/**
	 * 删除商品
	 * @param model
	 * @param productId
	 * @return
	 */
	@ResponseBody
	//@RequiresPermissions("system:goods:list:delete")
	@DeleteMapping(value = "/list/delete/{id}")
	public ResultBean delete(Model model, @PathVariable("id") Long productId ) {
		ResultBean result = new ResultBean();
		if(productId!=null && !productId.equals("")){
			try {
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("product_id", productId);
				
				productService.deleteById(productId);
				productImageService.deleteByMap(map);
				productDetailService.deleteByMap(map);
				productCategoryService.deleteByMap(map);
				productParameterService.deleteByMap(map);
				productAttributeService.deleteByMap(map);
				productSpecificationService.deleteByMap(map);
				
				result.setCode(1);
				result.setSuccess(true);
				result.setMessage("已删除");
			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(0);
				result.setSuccess(false);
				result.setMessage("已删除");
			}
		}else{
			result.setCode(1);
			result.setSuccess(true);
			result.setMessage("已删除");
		}
		
		return result;
	}
	

	
	/**
	 * 添加商品
	 * @param model
	 * @param jsonParam
	 * @return
	 */
	@ResponseBody
	//@RequiresPermissions("system:goods:list:add")
	@PostMapping(value = {"/list/add" },produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResultBean add(Model model, @RequestBody JSONObject jsonParam) {
		
		String userName = null;
		
		ResultBean result = new ResultBean();
		
		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		if(authorizingUser!=null){
			userName = authorizingUser.getLoginName();
		}else{
			result.setCode(0);
			result.setSuccess(false);
			result.setMessage("未登录或登陆超时，请重新登陆");
			return result;
		}
		
		if(!jsonParam.isEmpty()){
			try {
				Date dateTime = new Date();
				Long productNumber = System.currentTimeMillis();
				OsProduct oPentity = new OsProduct();
				OsProductVo product = jsonParam.toJavaObject(OsProductVo.class);
				PropertyUtils.copyProperties(oPentity, product);
				oPentity.setCreateBy(userName);
				oPentity.setCreateTime(dateTime);
				oPentity.setShelveBy(userName);
				oPentity.setShelveTime(dateTime);
				oPentity.setUpdateBy(userName);
				oPentity.setUpdateTime(dateTime);
				oPentity.setProductNumber(productNumber);
				productService.insert(oPentity);
				
				result.setCode(0);
				result.setSuccess(true);
				result.setMessage("未登录或登陆超时，请重新登陆");
				
			} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
				e.printStackTrace();
				result.setCode(0);
				result.setSuccess(false);
				result.setMessage("未登录或登陆超时，请重新登陆");
			}
			
		}else{
			result.setCode(0);
			result.setSuccess(false);
			result.setMessage("未登录或登陆超时，请重新登陆");
		}
		
		return result;
	}
	
	
	
}
